package o;

import android.text.TextUtils;
import com.huawei.hms.framework.common.ContainerUtils;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public final class enw {
    public static env ˏ(String str, String str2) {
        env o_env = new env(str);
        try {
            Document parse = Jsoup.parse(str2);
            ˋ(o_env, parse);
            ˊ(o_env, parse);
            ˎ(str, o_env, parse);
            ˏ(o_env, parse);
            ˎ(o_env, parse);
            String ˎ = enx.ˎ(str);
            if (TextUtils.isEmpty(ˎ)) {
                ॱ(str, o_env, parse);
            } else {
                o_env.ˎ(ˎ);
            }
        } catch (Exception e) {
            Logger.e("WebInfoParser", "parse error");
        }
        return o_env;
    }

    public static env ˋ(String str, String str2) {
        env o_env = new env(str);
        try {
            ॱ(str, o_env, Jsoup.parse(str2));
        } catch (Exception e) {
            Logger.e("WebInfoParser", "parse error");
        }
        return o_env;
    }

    private static void ˎ(env o_env, Document document) {
        int i = -1;
        String ˊ = ˊ(document, "share_flag");
        if (!TextUtils.isEmpty(ˊ)) {
            try {
                i = Integer.parseInt(ˊ);
            } catch (NumberFormatException e) {
                Logger.e("WebInfoParser", "error shareType:" + ˊ);
            }
        }
        o_env.ˎ(i);
    }

    private static void ˏ(env o_env, Document document) {
        int i = 1;
        String ˊ = ˊ(document, "share_type");
        if (!TextUtils.isEmpty(ˊ)) {
            try {
                i = Integer.parseInt(ˊ);
            } catch (NumberFormatException e) {
                Logger.e("WebInfoParser", "error shareType:" + ˊ);
            }
        }
        o_env.ॱ(i);
    }

    private static void ˎ(String str, env o_env, Document document) {
        Object ˊ = ˊ(document, "higame-image");
        if (TextUtils.isEmpty(ˊ)) {
            ˊ = ॱ(document, str);
            if (!TextUtils.isEmpty(ˊ)) {
                o_env.ˋ(ˊ);
                return;
            }
            return;
        }
        o_env.ˋ(ˊ);
    }

    private static void ˊ(env o_env, Document document) {
        Object ˊ = ˊ(document, "description");
        if (!TextUtils.isEmpty(ˊ)) {
            o_env.ˏ(ˊ);
        }
    }

    private static void ˋ(env o_env, Document document) {
        Elements select = document.select("meta[name=higame-share-title]");
        if (select == null || select.size() <= 0) {
            String title = document.title();
            if (!TextUtils.isEmpty(title)) {
                o_env.ॱ(title);
                return;
            }
            return;
        }
        o_env.ॱ(select.attr("content"));
    }

    private static String ˊ(Document document, String str) {
        Elements select = document.select("meta[name=" + str + "]");
        if (select != null) {
            return select.attr("content");
        }
        return null;
    }

    private static String ॱ(Document document, String str) {
        String ˊ = ˊ(str);
        Elements select = document.select("img");
        String str2 = null;
        if (select == null || select.size() <= 0) {
            return null;
        }
        Iterator it = select.iterator();
        while (it.hasNext()) {
            Element element = (Element) it.next();
            CharSequence ˎ = ˎ(element, ˊ);
            if (!TextUtils.isEmpty(ˎ)) {
                return ˎ;
            }
            str2 = ˊ(ˊ, element);
            if (!TextUtils.isEmpty(str2)) {
                return str2;
            }
        }
        return str2;
    }

    private static String ˊ(String str) {
        try {
            URL url = new URL(str);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(url.getProtocol()).append("://");
            stringBuilder.append(url.getAuthority());
            return stringBuilder.toString();
        } catch (MalformedURLException e) {
            Logger.e("WebInfoParser", "getBaseUrl error");
            return null;
        }
    }

    private static String ˏ(Element element, String str) {
        if (element.hasAttr("src")) {
            String attr = element.attr("src");
            if (attr != null) {
                if (attr.startsWith("//")) {
                    attr = "http:" + attr;
                } else if (attr.startsWith("/")) {
                    attr = str + attr;
                } else if (!Pattern.compile("^(http://|https://)", 2).matcher(attr).find()) {
                    attr = str + "/" + attr;
                }
                return attr.replaceAll("&amp;", ContainerUtils.FIELD_DELIMITER);
            }
        }
        return null;
    }

    private static String ˎ(Element element, String str) {
        if (!element.hasAttr("id")) {
            return null;
        }
        String attr = element.attr("id");
        if (attr == null || !attr.startsWith("aimg_")) {
            return null;
        }
        return ˏ(element, str);
    }

    private static String ˊ(String str, Element element) {
        String ˏ = ˏ(element, str);
        return (ˋ("width", element) < 180 && ˋ("img_width", element) < 180 && ˋ("height", element) < 180 && ˋ("img_height", element) < 180) ? null : ˏ;
    }

    private static int ˋ(String str, Element element) {
        int i = -1;
        if (element.hasAttr(str)) {
            String attr = element.attr(str);
            if (attr != null) {
                try {
                    i = Integer.parseInt(attr);
                } catch (NumberFormatException e) {
                    Logger.e("WebInfoParser", "getImgUrl, width error, width:" + attr);
                }
            }
        }
        return i;
    }

    private static void ॱ(String str, env o_env, Document document) {
        CharSequence ˊ = ˊ(document, "appgallery-share-url");
        Logger.ˏ("WebInfoParser", "webview meta Url isEmpty? " + TextUtils.isEmpty(ˊ));
        if (!TextUtils.isEmpty(ˊ)) {
            CharSequence charSequence = ˊ;
        }
        o_env.ˎ(str);
    }
}
